const app = getApp();

Page({
  data: {
    code: '',
    token: '',
    outline: false,
    hidden: false,
  },
  onLoad(query) {
    // 页面加载


  },
  onReady() {
    // 页面加载完成
  },
  onShow() {
    var that = this;
    my.getNetworkType({
      success: (res) => {
        if (res.networkAvailable) {
          that.setData({
            outline: false,
            todos: []
          })
          // 获取token
          my.getAuthCode({
            scopes: 'auth_user', //授权类型，默认auth_base(静默授权)，支持auth_base/auth_user(主动授权)
            success: (res) => {
              if (res.authCode) { // 认证成功
                my.httpRequest({ // 成功之后调用后台开启设备接口
                  url: 'https://www.tiantong369.com/gettoken',
                  method: 'POST',
                  data: res.authCode,
                  success: (res) => {
                    if (res.data.status == 200) {
                      this.setData({
                        token: res.data.token
                      })
                    }
                  },
                  fail: (res) => {
                    console.log('fail:', res)
                  },
                });
              }
            },

          })

        } else {
          that.setData({
            outline: true
          })
        }
      }
    })
  },
  startSetInter: function() {
    var that = this;
    that.setData({
      hidden: true
    })
    //将计时器赋值给setInter
    that.data.setInter = setInterval(function() {
      that.data.deviceid = 'M2018000219';
      var req = {
        deviceid: '' + that.data.deviceid + '',
      }
      req = JSON.stringify(req)
      httpUtil.post("/crm/GetEmpPrice", req)
        .then(function(res) {
          console.log(that.data.status)
          console.log(that.data.status1)
          if (res.data.device.status == 1) {
            clearInterval(that.data.setInter);
            that.pay();
          } else {
            that.data.status1++;
          }
          if (that.data.status1 >= 30) {
            that.setData({
              hidden: false
            })
            clearInterval(that.data.setInter);
            that.setData({
              hidden: false
            })
            wx.showToast({
              title: '设备离线中。。。',
              icon: 'none',
              duration: 2000
            })
          }
        }).catch(function(res) {
          wx.showToast({
            title: '网络异常',
            icon: 'none',
            duration: 1500
          });
        })
    }, 1000);

  },
  onHide() {
    // 页面隐藏
  },
  onUnload() {
    // 页面被关闭
  },
  onTitleClick() {
    // 标题被点击
  },
  onPullDownRefresh() {
    // 页面被下拉
  },
  onReachBottom() {
    // 页面被拉到底部
  },
  bindKeyInput(e) {
    this.setData({
      code: e.detail.value
    });
  },
  onShareAppMessage() {
    // 返回自定义分享信息
    return {
      title: 'My App',
      desc: 'My App description',
      path: 'pages/index/index',
    };
  },
  str() {
    var that=this;
    that.setData({
      hidden: true,
    })
    var req = {
      deviceid: app.deviceid,
      TryCode: this.data.code
    }
    my.httpRequest({ // 调用自己的服务端接口，让服务端进行后端的授权认证，并且种session，需要解决跨域问题
      url: 'https://www.tiantong369.com/crm/devicetest', // 该url是自己的服务地址
      method: 'POST',
      data: req,
      headers: {
        'x-access-token': this.data.token
      },
      success: (res) => {// 成功获取订单号
        console.log(res.data)
        that.setData({
          hidden: false,
        })
        my.alert({ content: res.data.message });

      },
      fail: (res) => {
        console.log('/Wxpay_callback:', res)
      },
    });
  }
});
